<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style//css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style//css/transaction/add_form.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/project_device_alert.css">
</script>
<div>
    <div class="layui-form " style="padding: 50px;" lay-filter="layuiadmin-form" id="layuiadmin-form">
        <div class="layui-table-box" style="padding: 50px;">
            <ul class="layui-row ">
                <li class="layui-col-md4 layui-col-sm4">
                    <div class="
                    form layui-form-item">
                        <label class=" 
                    layui-label-inlineblock    layui-form-label"><span style="font-size: 18px;color: red;position: relative;display: inline-block;line-height: 20px;vertical-align: middle;top:3px;">*</span>工单名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="tcReqName" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>
                    <div class="
                    form layui-form-item">
                        <label class=" 
                    layui-label-inlineblock    layui-form-label">OA工单编号：</label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                    <div class="
                    form layui-form-item">
                        <label class=" 
                         layui-label-inlineblock    layui-form-label">申请日期：<span id="time"></span></label>
                        <div class="layui-input-block">
                        </div>
                    </div>
                </li>
                <li class="layui-col-md4 layui-col-sm4">
                    <div class="
                    form layui-form-item">
                        <label>工单编号：</label>
                        <div class="layui-input-block" id="bianhao" style='display: flex;align-items: center;'>
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock  layui-form-label"><span style="font-size: 18px;color: red;position: relative;display: inline-block;line-height: 20px;vertical-align: middle;top:3px;">*</span>申请借调公司：</label>
                        <div class="layui-input-block">
                            <select id="tcReqCoalt" name="tcReqCoalt" placeholder="请输入申请借调公司" lay-search lay-verify="required">
                            </select>
                        </div>
                    </div>
                    <div class="
                    form layui-form-item">
                        <label class=" 
                    layui-label-inlineblock    layui-form-label"><span style="font-size: 18px;color: red;position: relative;display: inline-block;line-height: 20px;vertical-align: middle;top:3px;">*</span>备件所属公司：</label>
                        <div class="layui-input-block">
                            <select id="tcReqCoorig" name="tcReqCoorig" placeholder="请输入备件所属公司：" lay-search lay-verify="required">
                            </select>
                        </div>
                    </div>
                </li>
                <li class="layui-col-md4 layui-col-sm4" style="width: 120px;margin-left: 60px;">
                    <div class="form layui-form-item">
                        <label class=" layui-label-inlineblock   layui-form-label">工单状态：</label>
                        <div class="layui-input-block" style='display: flex;align-items: center;'>
                            未提交
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock    layui-form-label"><span style="font-size: 18px;color: red;position: relative;display: inline-block;line-height: 20px;vertical-align: middle;top:3px;">*</span>申请人：</label>
                        <div id="gen01" class="layui-input-block">
                        </div>
                    </div>
                    <div class="form layui-form-item">
                        <label class="layui-label-inlineblock    layui-form-label">借调日期：</label>
                        <div id="gen01" class="layui-input-block">
                            <input type="text" name="fil12" autocomplete="off" class="layui-input" id="jieTime" placeholder="请输入借调日期">
                        </div>
                    </div>
                </li>
            </ul>
            <div class="layui-table-box">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">调拨说明:</label>
                    <div class="layui-input-block">
                        <textarea name="fil21" placeholder="请输入调拨说明" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注：</label>
                    <div class="layui-input-block">
                        <textarea name="fil07" placeholder="请输入备注" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="title-btn layui-btn-containner">
            <button class="add layui-btn layui-btn-radius layui-btn-primary">
                <i class="add-icon layui-icon layui-icon-add-circle"></i>
                <span class="btn-inner">添加备件</span>
            </button>

        </div>
        <div class="layui-card-body"></div>
        <table class="layui-table" lay-size="sm" id="baseTable">
            <thead>
                <tr>
                     <th>操作</th>
                     <th>备品备件编号</th>
                     <th>备品备件品名</th>
                     <th>规格</th>
                     <th>单位</th>
                     <th>借调数量</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <div class="footer layui-btn-containner">
            <button class="commit layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-tj">提交</button>
            <button class="hold layui-btn layui-btn-radius layui-btn-primary" lay-submit
                lay-filter="LAY-submit-bc">保存</button>
            <button class="cancel layui-btn layui-btn-radius layui-btn-primary">取消</button>
        </div>
    </div>
</div>
<script>
    var shuju = {
        filFileSpFimParams: []
    }
    layui.use(['form', 'table', 'api', 'utill', 'laydate', 'jquery'], function () { //此段代码必不可少
        var table = layui.table,
            setter = layui.setter,
            laytpl = layui.laytpl,
            api = layui.api,
            utill = layui.utill,
            form = layui.form,
            admin = layui.admin,
            view = layui.view,
            element = layui.element,
            form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate;
        // 借调时间
            laydate.render({
                elem: '#jieTime', // 指定元素
                type:'datetime',
                trigger: 'click'
          });
        $('[data-jump="set/sparecommit"]').addClass('layui-this').attr('color', '#FFF');
        form.render(null, 'layuiadmin-form');
        // 取消
        $(".cancel").click(function(){
            location.hash = '/set/sparecommit'
        })
        form.on('submit(LAY-submit-tj)', function (data) {
            var field = data.field
            shuju.fil01 = $('#bianhao').html()
            shuju.fil05 = 1
            shuju.fil07 = field.fil07
            shuju.fil08 = $('#gen01').html().split('-')[1]
            shuju.fil21 = field.fil21
            shuju.tcReqCoorig = field.tcReqCoorig
            shuju.tcReqCoalt = field.tcReqCoalt
            shuju.tcReqName = field.tcReqName
            tj(JSON.stringify(shuju))
        })
        form.on('submit(LAY-submit-bc)', function (data) {
            
            var field = data.field
            if(field.tcReqCoorig == field.tcReqCoalt){
                layer.open({content:"申请借调公司与备件所属公司不能相同"});
                return;
            }
            shuju.fil01 = $('#bianhao').html()
            shuju.fil05 = 0
            shuju.fil07 = field.fil07
            shuju.fil12 = field.fil12
            shuju.fil08 = $('#gen01').html().split('-')[1]
            shuju.fil21 = field.fil21
            shuju.tcReqCoorig = field.tcReqCoorig
            shuju.tcReqCoalt = field.tcReqCoalt
            shuju.tcReqName = field.tcReqName
            bc(JSON.stringify(shuju))
        })
        function bc(data1) {
            utill.loading.show();
            admin.req({
                url: api.host + api.FilFileBPUpdateFilIma2,
                type: "post",
                data: data1,
                dataType: 'json',
                contentType: 'application/json',
                done: function (res) {
                    utill.loading.hide();
                    if(res.code == 0){
                        layer.msg(res.message)
                       setTimeout(function(){
                            location.hash = '/set/sparecommit'
                       },1000);
                    }else{
                        layer.msg(res.message)
                    }
                }
            });
        }
        function tj(data1) {
            utill.loading.show();
            admin.req({
                url: api.host + api.FilFileBPUpdateFilIma,
                type: "post",
                data: data1,
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    utill.loading.hide();
                    layer.closeAll();
                    if(res.code == 0){
                        layer.msg(res.message)
                        setTimeout(function(){
                            location.hash = '/set/sparecommit'
                        },1000)
                    }else{
                        layer.msg(res.message)
                    }
                }
            });
        }
        document.getElementById('time').innerHTML = utill.timestampToTime(new Date())
        admin.req({
            url: api.host + api.AzpSelectAllAzp,
            type: "POST",
            done: function (res) {
                var str = '<option value=""></option>'
                for (let index = 0; index < res.data.length; index++) {
                    str = str +
                        `<option value="${res.data[index].azp01}">${res.data[index].azp02}</option>`
                }
                $('#tcReqCoorig').html(str)
                $('#tcReqCoalt').html(str)
                form.render(null, 'layuiadmin-form');
            }
        });
        admin.req({
            url: api.host + api.FilFileDBInsertFilFileMove1,
            type:'post',
            done: function (res) {
                $('#bianhao').html(res.data.str);
                $('#gen01').html(res.data.name[0].gen02 + "-" + res.data.name[0].gen01);
            }
        });
        $('#baseTable').on('click', '.table-delete', function () {
            var index = $(this).parents('div').parents('td').parents('tr').index()
            shuju.filFileSpFimParams.splice(index, 1);
            $('#baseTable').find('> tbody tr').eq(index).remove();
        });
        $('#baseTable').on('click', '.table-edit', function () {
            var trindex = $(this).parents('div').parents('td').parents('tr').index()
            admin.popup({
                title: '编辑设备信息',
                area: ['600px', '250px'],
                id: 'LAY-popup-content-tags',
                success: function (layero, index){
                    view(this.id).render('set/set_alert/bianji')
                        .done(function () {
                            form.val("layuiadmin-form-tags", {
                                "ima01": $('#baseTable').find('> tbody').find(
                                    'tr').eq(trindex).find('td').eq(1)
                                    .html(),
                                "ima02": $('#baseTable').find('> tbody').find(
                                    'tr').eq(trindex).find('td').eq(2)
                                    .html(),
                                "ima021": $('#baseTable').find('> tbody').find(
                                    'tr').eq(trindex).find('td').eq(3)
                                    .html(),
                                "ima25": $('#baseTable').find('> tbody').find(
                                    'tr').eq(trindex).find('td').eq(4)
                                    .html(),
                                "tcDetailNum": $('#baseTable').find('> tbody')
                                    .find('tr').eq(trindex).find('td').eq(5)
                                    .html()
                            })
                            form.on('submit(LAY-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                var str = `
                                <td >
                                    <div> <span class="table-edit"  ></span>
                                    <span  class="table-delete" ></span></div>
                                    </td>
                                      <td>${field.ima01}</td>
                                      <td>${ field.ima02}</td>
                                      <td>${ field.ima021}</td>
                                      <td>${ field.ima25}</td>
                                      <td>${field.tcDetailNum}</td>
                                 `
                                shuju.filFileSpFimParams[trindex].tcDetailNum = field.tcDetailNum
                                $('#baseTable').find('> tbody')
                                    .find('tr').eq(trindex)
                                    .html(str)
                                layer.close(index); //执行关闭 
                                form.render()
                            });
                        });
                }
            });

        });
        var arr = [];
        $('.title-btn').on('click', '.add', function (e) {
            if (!$('#tcReqCoorig').val()) {
                layer.msg("请选择设备原公司");
                return false
            }else if($("#tcReqCoorig").val() != $("#tcReqCoalt").val()){
                sessionStorage.setItem('tcReqCoalt', $("#tcReqCoorig").val())
                    admin.popup({
                        title: '添加备件',
                        area: ['600px', '300px'],
                        id: 'LAY-popup-content-tags',
                        success: function (layero, index) {
                            view(this.id).render('set/set_alert/alert')
                                .done(function () {
                                    //监听提交
                                    form.on('submit(LAY-submit)', function (data) {
                                        $("#tcReqCoorig").attr('disabled','disabled');
                                        $("#tcReqCoalt").attr('disabled','disabled');
                                        form.render()
                                        var field = data.field; //获取提交的字段
                                        field.ima01 = field.ima01.split("/")[0]
                                        if(arr == ''){
                                            var str = `
                                        <tr>
                                        <td >
                                            <div> <span class="table-edit"  ></span>
                                            <span  class="table-delete" ></span></div>
                                            </td>
                                            <td>${field.ima01}</td>
                                            <td>${ field.ima02}</td>
                                            <td>${ field.ima021}</td>
                                            <td>${ field.ima25}</td>
                                            <td>${field.tcDetailNum}</td>
                                            </tr>
                                        `
                                        $('#baseTable').find('> tbody').append(str);
                                        shuju.filFileSpFimParams.push({
                                            fim01: $('#bianhao').html(),
                                            tcDetailFia01: field.ima01,
                                            tcDetailNum: field.tcDetailNum
                                        })
                                        arr.push(field.ima01);
                                        }else{
                                            arr.forEach(elem=>{
                                            if(field.ima01 == elem){
                                                arr.length = 0;
                                                arr = [];
                                                layer.msg('请勿添加重复备品备件!')
                                            }else{
                                                var str = `
                                                <tr>
                                                <td >
                                                    <div> <span class="table-edit"  ></span>
                                                    <span  class="table-delete" ></span></div>
                                                    </td>
                                                    <td>${field.ima01}</td>
                                                    <td>${ field.ima02}</td>
                                                    <td>${ field.ima021}</td>
                                                    <td>${ field.ima25}</td>
                                                    <td>${field.tcDetailNum}</td>
                                                    </tr>
                                                `
                                                $('#baseTable').find('> tbody').append(str);
                                                shuju.filFileSpFimParams.push({
                                                    fim01: $('#bianhao').html(),
                                                    tcDetailFia01: field.ima01,
                                                    tcDetailNum: field.tcDetailNum
                                            })
                                            arr.push(field.ima01);
                                            }
                                        })
                                        }
                                        
                                        
                                        layer.close(index); //执行关闭 
                                    });
                                });
                        }
                    });
            }else if($("#tcReqCoorig").val() == $("#tcReqCoalt").val()){
                 layer.msg("申请借调公司不能与备件所属公司相同")
                 form.render("select")
                 return;
            }
        })
    });
</script>